package com.diy.sigmund.junit5.mockito;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.internal.invocation.InterceptedInvocation;
import org.mockito.internal.stubbing.answers.Returns;
import org.mockito.internal.verification.Times;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

/**
 * 以前使用PowerMockito来mock静态方法，自Mockito 3.4.0 以后不再需要PowerMockito, Mockito本身已经支持
 * <br>
 * <code>https://javadoc.io/doc/org.mockito/mockito-core</code>
 */
public class MockStaticDemo {

    @Test
    void testStatic1() {
        MockedStatic<StaticDemo> m = Mockito.mockStatic(StaticDemo.class);
        // mock不带参数的静态方法
        m.when(StaticDemo::hello).thenReturn("mock-hello").thenCallRealMethod();
        Assertions.assertEquals(StaticDemo.hello(), "mock-hello");
        Assertions.assertEquals(StaticDemo.hello(), "hello");
        m.verify(StaticDemo::hello, new Times(2));
        m.close();
    }

    @Test
    void testStatic11() {
        try (MockedStatic<StaticDemo> mockedStatic = Mockito.mockStatic(StaticDemo.class)) {
            // mock带参数的静态方法
            mockedStatic.when(() -> StaticDemo.hi(ArgumentMatchers.anyString())).thenReturn("mock-hi");
            Assertions.assertEquals(StaticDemo.hi(ArgumentMatchers.anyString()), "mock-hi");
        }
    }

    @Test
    void testStatic2() {
        MockedStatic<StaticDemo> m = Mockito.mockStatic(StaticDemo.class, new Returns("mock-returns"));
        Assertions.assertEquals(StaticDemo.hello(), "mock-returns");
        Assertions.assertEquals(StaticDemo.hi("tom"), "mock-returns");
        m.close();
    }

    @Test
    void testStatic3() {
        MockedStatic<StaticDemo> m = Mockito.mockStatic(StaticDemo.class, invocation -> {
            Object[] args = invocation.getArguments(); // 调用方法的参数
            Object mock = invocation.getMock(); // 被mock的静态类的class信息，不是静态类本身
            Method method = invocation.getMethod(); // 被调用的具体方法
            String location = ((InterceptedInvocation) invocation).getLocation().toString(); // 调用具体位置
            System.out.println(location + " invoke method: " + method.getName()
                    + " with arguments: " + Arrays.toString(args)
                    + " return type: " + method.getReturnType().getName()
            );
            if (Objects.equals(method.getName(), "hello")) {
                return "mock-hello";
            } else if (Objects.equals(method.getName(), "hi")) {
                return "mock-hi" + Arrays.toString(args);
            }
            return location + " " + method.getName() + " called with arguments: " + Arrays.toString(args);
        });
        Assertions.assertEquals(StaticDemo.hello(), "mock-hello");
        System.out.println(StaticDemo.hi("cat"));
        System.out.println(StaticDemo.hi("dog"));
        System.out.println(StaticDemo.hi("dog"));
        System.out.println(StaticDemo.eat("elephant"));
        m.verify(StaticDemo::hello, new Times(1));
        m.verify(() -> {
            StaticDemo.hi("cat"); // 验证方法 hi() 调用参数cat 只调用了1次
        }, new Times(1));
        m.verify(() -> {
            StaticDemo.hi("dog"); // 验证方法 hi() 调用参数dog 只调用了2次
        }, new Times(2));
        m.verify(() -> {
            StaticDemo.hi(ArgumentMatchers.anyString()); // 验证方法 hi() 调用参数任意string串 调用了3次
        }, new Times(3));
        m.close();
    }


}
class StaticDemo {
    private String str = "222";

    static String hello() {
        return "hello";
    }

    static String hi(String name) {
        return "hi " + name;
    }

    static String eat(String name) {
        return "eat " + name;
    }
}
